//
//  MapViewController.swift
//  SidebarMenu
//
//  Created by Simon Ng on 2/2/15.
//  Copyright (c) 2015 AppCoda. All rights reserved.
//

import UIKit

private let kItemMargin : CGFloat = 10
private let kHeaderViewH : CGFloat = 10
private let NormalCellID = "NormalCellID"
private let HeaderViewID = "HeaderViewID"
let kNormalItemW = (UIScreen.main.bounds.width - 4 * kItemMargin) / 3
let kNormalItemH = kNormalItemW * 3 / 4
let PrettyCellID = "PrettyCellID"

class MapViewController: UIViewController {
    @IBOutlet weak var menuButton:UIBarButtonItem!
    var contentView : UIView?
    var upView     : UIView?
    
    lazy var topView : UIView = {[unowned self] in
        
        let rect = CGRect(x: 0, y: 72, width: self.view.bounds.width, height: 40)
        let topView = UIView(frame: rect)
        topView.backgroundColor = UIColor.green
        let rect1 = CGRect(x: 15, y: 4, width: 30, height: 32)
        let rect2 = CGRect(x: 55, y: 4, width: 120, height: 32)
        let rect3 = CGRect(x: 220, y: 4, width: 30, height: 32)
        let rect4 = CGRect(x: 260, y: 4, width: 120, height: 32)
        let imgView = UIImageView(frame:rect1)
        imgView.backgroundColor = UIColor.yellow
        let label1 = UILabel(frame:rect2)
        label1.text = "测量中：100/100"
        label1.font = UIFont.boldSystemFont(ofSize: 15)
        label1.textAlignment = NSTextAlignment.left
        let imgView1 = UIImageView(frame:rect3)
        imgView1.backgroundColor = UIColor.yellow
        topView.addSubview(imgView1)
        let label2 = UILabel(frame:rect4)
        label2.text = "未写结论：999"
        label2.font = UIFont.boldSystemFont(ofSize: 15)
        label2.textAlignment = NSTextAlignment.left
        topView.addSubview(imgView)
        topView.addSubview(imgView1)
        topView.addSubview(label1)
        topView.addSubview(label2)
        return topView
        
        }()

    lazy var collectionView : UICollectionView = {[unowned self] in
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: kNormalItemW, height: kNormalItemH)
        layout.minimumLineSpacing = kItemMargin
        layout.minimumInteritemSpacing = kItemMargin
        layout.headerReferenceSize = CGSize(width: UIScreen.main.bounds.width, height: kHeaderViewH)
        layout.sectionInset = UIEdgeInsets(top: 0, left: kItemMargin, bottom: 0, right: kItemMargin)
        let rect = CGRect(x: 0, y: 112, width: self.view.bounds.width, height: self.view.bounds.height-112)
        let collectionView = UICollectionView(frame: rect, collectionViewLayout: layout)
        collectionView.backgroundColor = UIColor.white
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
        
        collectionView.register(CollectionNormalCell.self, forCellWithReuseIdentifier: NormalCellID)
        
        return collectionView
        }()

    override func viewDidLoad() {
        super.viewDidLoad()

        if revealViewController() != nil {
            menuButton.target = revealViewController()
            menuButton.action = #selector(SWRevealViewController.revealToggle(_:))
            view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
        }
        setupUI()
//        loadData()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}


extension MapViewController {
     func setupUI() {
        upView = topView
        view.addSubview(upView!)
        contentView = collectionView
        view.addSubview(collectionView)
    }
}

extension MapViewController {
    func loadData() {
    }
}
extension MapViewController : UICollectionViewDataSource {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: NormalCellID, for: indexPath) as! CollectionNormalCell
//        cell.anchor = baseVM.anchorGroups[indexPath.section].anchors[indexPath.item]
        return cell
    }
    
//    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
//        let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: HeaderViewID, for: indexPath) as! CollectionHeaderView
//        headerView.group = baseVM.anchorGroups[indexPath.section]
//        return headerView
//    }
    
}

extension MapViewController : UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
     print(indexPath.row)
    }
    
//    private func presentShowRoomVc(_ anchor : AnchorModel) {
//        let showVc = ShowRoomVC()
//        showVc.anchor = anchor
//        present(showVc, animated: true, completion: nil)
//    }
//    
//    private func pushNormalRoomVc(_ anchor : AnchorModel) {
//        let normalVc = NormalRoomVC()
//        normalVc.anchor = anchor
//        navigationController?.pushViewController(normalVc, animated: true)
//    }
}
